<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<jk-add-button :permission-prefix="prefix" class="margin-5-0 item-gap" @click="onAddBtn"></jk-add-button>
					<jk-button size="small" type="warning" icon="el-icon-setting" @click="batchSetSignalDialogState = true">批量设置点位</jk-button>
					<!--<jk-report-button :permission-prefix="prefix" class="margin-5-0" :loading="exportLoading" @click="onReportBtn"></jk-report-button>-->
				</div>
				<div class="flex-end-center-wrap">
					<jk-select v-model="searchParams.auditState" :option-list="auditStateList" placeholder="状态" clearable size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<jk-select v-model="searchParams.workshopId" :option-list="workshopList" clearable placeholder="区域" size="small" class="search-bar-item-size2 margin-5-0 item-gap"></jk-select>
					<jk-select v-model="searchParams.processId" :option-list="processList" clearable size="small" placeholder="工序" class="search-bar-item-size1 item-gap"></jk-select>
					<jk-select v-model="searchParams.typeCode" value-key-name="code" :option-list="machineTypeList" clearable size="small" placeholder="设备类型" class="search-bar-item-size1 item-gap"></jk-select>
					<jk-search-input v-model="searchParams.nameOrCode" placeholder="编码或名称" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable @on-enter="onSearchBtn" />
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table :loading="tableLoading" :data="tableData" :height="tableHeight + 20">
				<vxe-column title="设备编码" align="left" field="code" :min-width="100" />
				<vxe-column title="设备名称" align="left" field="name" :min-width="100" />
				<vxe-column title="区域" align="left" field="workshopName" :min-width="100" />
				<vxe-column title="设备类型" align="left" field="typeName" :width="100" />
				<vxe-column title="设备机型" align="left" field="machineModelName" :width="120" />
				<vxe-column title="工序" align="center" field="processName" :width="100" />
				<vxe-column title="是否单锭" align="center" field="ifSpinOutput" :width="80">
					<template slot-scope="scope">
						<el-tag :type="scope.row.ifSpinOutput ? 'primary' : 'info'" size="mini">{{ scope.row.ifSpinOutput ? '是' : '否' }}</el-tag>
					</template>
				</vxe-column>
				<vxe-column title="是否排产" align="center" field="ifScheduling" :width="80">
					<template slot-scope="scope">
						<el-tag :type="scope.row.ifScheduling ? 'primary' : 'info'" size="mini">{{ scope.row.ifScheduling ? '是' : '否' }}</el-tag>
					</template>
				</vxe-column>
				<vxe-column title="是否监控" align="center" field="ifMonitoring" :width="80">
					<template slot-scope="scope">
						<el-tag :type="scope.row.ifMonitoring ? 'primary' : 'info'" size="mini">{{ scope.row.ifMonitoring ? '是' : '否' }}</el-tag>
					</template>
				</vxe-column>
				<vxe-column title="表数单位" align="center" field="ifMonitoring" :width="80">
					<template slot-scope="scope">
						<el-tag :type="scope.row.ifMonitoring ? 'primary' : 'info'" size="mini">{{ scope.row.useMachineOutputKg ? '公斤' : '米' }}</el-tag>
					</template>
				</vxe-column>
				<vxe-column title="排序" align="center" field="sortNum" :width="80" />
				<vxe-column title="创建人" align="left" field="creatorName" width="90"></vxe-column>
				<vxe-column title="创建时间" align="left" field="createTime" width="160"></vxe-column>
				<vxe-column title="修改人" align="left" field="updaterName" width="90"></vxe-column>
				<vxe-column title="修改时间" align="left" field="updateTime" width="160"></vxe-column>
				<vxe-column title="状态" align="center" field="auditState" :width="100">
					<template slot-scope="scope">
						<jk-common-status :status="scope.row.auditState"></jk-common-status>
					</template>
				</vxe-column>
				<vxe-column title="操作" align="center" fixed="right" :width="250">
					<template slot-scope="scope">
						<jk-operation-bar
							:scope="scope"
							api-prefix="machinemanager/machine"
							:permission-prefix="prefix"
							@on-detail="onDetailBtn"
							@on-edit="onEditBtn"
							@on-refresh="getListRequest"
						></jk-operation-bar>
					</template>
				</vxe-column>
			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
		<detail-dialog
			:data-id="dataId"
			:dialog-state="detailDialogState"
			@on-visible-change="onVisibleChangeOfDetailDialog"
		></detail-dialog>
		<save-dialog
			:permission-prefix="prefix"
			:data-id="dataId"
			:dialog-state="saveDialogState"
			@on-visible-change="onVisibleChange"
			@on-finish="onFinish"
		></save-dialog>
		<batch-set-signal-dialog
			:dialog-state="batchSetSignalDialogState"
			@on-visible-change="onVisibleChangeOfBatchSetSignalDialog"
			@on-finish="onFinishOfBatchSetSignalDialog"
		></batch-set-signal-dialog>
	</div>
</template>

<script>
    import batchSetSignalDialog from './batch-set-signal-dialog';
    import saveDialog from './save-dialog';
    import detailDialog from './detail-dialog';
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig } from '/src/utils/jk/jk-tool';
    import {
        machinemanagerMachineDelete,
        machinemanagerMachinePage,
        machinemanagerMachineExportExcel
    } from '/src/api/machine/machineArchives';
    import { auditStateList } from '../../../../utils/jk/status-option-list';
    import { spinProcessPage } from '../../../../api/technology/process';
    import { systemBusinessDictPage } from '../../../../api/basicData/businessDict';

    export default {
        name: 'MachineArchives',
        components: {
            batchSetSignalDialog,
            saveDialog,
            detailDialog
        },
        mixins: [calHeight],
        data() {
            return {
                apiItem: {
                    'page': machinemanagerMachinePage,
                    'exportExcel': machinemanagerMachineExportExcel,
                    'delete': machinemanagerMachineDelete
                },
                batchSetSignalDialogState: false,
                machineTypeList: [],
                detailDialogState: false,
                prefix: 'personnel:group',
                auditStateList: auditStateList,
                dataId: null,
                workshopList: [],
                saveDialogState: false,
                tableLoading: true,
                exportLoading: false,
                tableData: [],
                totalCount: 0,
                processList: [],
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    nameOrCode: '',
                    typeCode: '',
                    auditState: null,
                    workshopId: null
                }
            };
        },
        mounted() {
            this.getDependentData();
        },
        methods: {
            onFinishOfBatchSetSignalDialog() {
                this.batchSetSignalDialogState = false;
                this.getListRequest();
            },
            onVisibleChangeOfBatchSetSignalDialog(e) {
                this.batchSetSignalDialogState = e;
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            getTypeListRequest() {
                return systemBusinessDictPage({ categoryCode: 'MACHINE_TYPE' }).then(res => {
                    if (res.data.status === 200) {
                        this.machineTypeList = res.data.res;
                    }
                });
            },
            async getDependentData() {
                this.tableLoading = true;
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                this.processList = this.$store.state.user.userProcess.processList;
                if (!this.searchParams.processId) {
                    this.$set(this.searchParams, 'processId', this.$store.state.user.userProcess.defaultProcessId);
                }
                if (!this.searchParams.workshopId) this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                await this.getTypeListRequest();
                await this.getListRequest();
                this.tableLoading = false;
            },
            onDetailBtn(e) {
                this.dataId = e.id;
                this.detailDialogState = true;
            },
            onVisibleChangeOfDetailDialog(e) {
                this.detailDialogState = e;
                if (!e) {
                    this.dataId = null;
                }
            },
            onFinish() {
                this.saveDialogState = false;
                this.getListRequest();
            },
            onVisibleChange(e) {
                this.saveDialogState = e;
                if (!e) {
                    this.dataId = null;
                }
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                return this.apiItem['page'](this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 新增按钮操作 */
            onAddBtn() {
                this.saveDialogState = true;
            },
            /** 修改按钮操作 */
            onEditBtn(row) {
                this.dataId = row.id;
                this.saveDialogState = true;
            },
            /** 导出按钮操作 */
            onReportBtn() {
                const params = { ...this.searchParams };
                params.pageNo = null;
                params.pageSize = null;
                this.$confirm('是否确认导出所有设备数据项?', { type: 'warning' }).then(() => {
                    this.exportLoading = true;
                    return this.apiItem['exportExcel'](params);
                }).then(res => {
                    if (res.data.status === 200) {
                        this.$download.excel(res, `${this.$route.meta.name}.xls`);
                        this.exportLoading = false;
                    }
                }).catch(() => {});
            }
        }
    };
</script>
